package com.hyg.module_testing.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.github.lzyzsd.jsbridge.DefaultHandler;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.module_testing.R;
import com.hyg.module_testing.databinding.FragmentTestingBinding;



/**
 * 检测页
 */
public class TestingFragment extends Fragment {
    FragmentTestingBinding binding;
    private String WebUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentTestingBinding.inflate(inflater);
        init();
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
//        StatusBarUtil.setStatusBar(1, getActivity(), getResources().getColor(R.color.bg), 0, null, null, true);
        String phone= UserSPUtils.getUserString(getActivity(), "phone");
        WebUrl = UrlUtils.TEST_LIST + phone + "&src=app&org=hyg";
        Log.i("lgb", "onResume  WebUrl: "+WebUrl);
//////  WebUrl = "http://app.tcmhyg.com/med/?src=app&org=hyg&userNumber="+sn.getString("phone","");
        binding.jsbridgewebview.loadUrl(WebUrl);
    }

    public void init() {
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        WebSettings webSettings = binding.jsbridgewebview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许h5使用javascript
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webSettings.setAllowContentAccess(false);
        webSettings.setAllowFileAccessFromFileURLs(false);
        webSettings.setAllowUniversalAccessFromFileURLs(false);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);//是否支持持久化存储，保存到本地
        webSettings.setAppCacheEnabled(true);
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //支持屏幕缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        //不显示webview缩放按钮
        webSettings.setDisplayZoomControls(false);
        // 设置可以使用localStorage
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        binding.jsbridgewebview.setDefaultHandler(new DefaultHandler());
        binding.jsbridgewebview.setWebChromeClient(new WebChromeClient());
//        webView.setWebViewClient(new MyWebViewClient());




        /**
         * js调用Android
         *
         *  参数一：getUserInfo就是注册供JS调用的方法名，
         *  参数二：data是JS传过来的参数，
         *  参数三：CallBackFunction 函数中需要把JS需要的response返回给JS
         */
        binding.jsbridgewebview.registerHandler("getToken", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.e("TAG", "js传递数据：" + data);
//                String token = getActivity().sn.getString("token", "");
                String token=UserSPUtils.getToken(getActivity());
                function.onCallBack(token);
            }
        });

        binding.webviewback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick())
                    return;
                if (binding.jsbridgewebview.canGoBack()) {
                    binding.jsbridgewebview.goBack();//返回上一页面
                } else {
                    binding.jsbridgewebview.loadUrl(WebUrl);
                }
                backWebView_showback();
            }
        });

    }

    public void backWebView_showback() {
        binding.jsbridgewebview.callHandler("functionInJs", "点击了返回键，处于原始页则隐藏", new CallBackFunction() {
            @Override
            public void onCallBack(String data) { //处理js回传的数据
                Log.e("TAG", "onCallBack:" + data);
                if (!Utils.isEmpty(data)) {
                    if (data.equals("1")) {
                        binding.webviewback.setVisibility(View.VISIBLE);
                    } else {
                        binding.webviewback.setVisibility(View.INVISIBLE);
                    }
                }
            }
        });
    }


}